﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NhanAi.DAO;
using NhanAi.Entity;
namespace NhanAi.Admin
{
    public partial class CreateTaxonomy : ListPage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            Authenticate();
            if (!IsPostBack)
            {
                TaxonomyDao dao = new TaxonomyDao();
                List<Taxonomy> list = dao.ReadAll();
                Taxonomy t = new Taxonomy();
                t.TermName = "[Trống]";
                list.Insert(0, t);
                cmbParent.DataSource = list;
                cmbParent.DataValueField = "Id";
                cmbParent.DataTextField = "TermName";
                cmbParent.DataBind();
            }
            SetCurrentPage("Taxonomy");
        }

        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {

        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            TaxonomyDao dao = new TaxonomyDao();
            Taxonomy t = new Taxonomy();
            try
            {
                t.ParentId = cmbParent.SelectedItem != null ? Int32.Parse(cmbParent.SelectedItem.Value) : 0;
                t.TermName = txtTermName.Text;
                t.TermNameEn = txtTermNameEn.Text;
                t.Description = txtDescription.Text;
                t.DescriptionEn = "";
                t.Count = 0;
                t.TaxonomyType = "taxonomy";
                dao.Create(t);
            }
            catch (Exception ex)
            {
                Session["message"] = "Đã xảy ra lỗi";
            }
            Session["message"] = "Thêm mới thành công";
        }
    }
}